<template>
  <div class="mod-config">
    <el-form>
      <el-form-item>
        <el-button-group>
          <el-button size="mini" v-if="isAuth('purchase:purchaseorderitem:save')" type="primary"
            @click="addOrUpdateHandle()">新增</el-button>
          <el-button size="mini" v-if="isAuth('purchase:purchaseorderitem:delete')" type="danger"
            @click="deleteHandle()" :disabled="dataListSelections.length <= 0">删除</el-button>
        </el-button-group>
        <el-button-group>
          <el-button size="mini" v-if="isAuth('purchase:purchaseorderitem:export')" type="primary" @click="exportData">
            导出</el-button>
          <el-button size="mini" v-if="isAuth('purchase:purchaseorderitem:import')" type="primary"
            @click="importData()">导入</el-button>
        </el-button-group>
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" size="mini" placeholder="参数名" clearable @clear="getDataList()"
          @blur="getDataList()"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="success" @click="getDataList()">查询</el-button>
        <el-button @click="resetForm" size="mini" type="info">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border size="mini" v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;" @cell-click="tableDbEdit" @cell-dblclick="tableDbEdit"
      :header-cell-style="{ background: '#fcfcfc', color: '#606266', height:'36px'}">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column type="index" header-align="center" align="center" label="#" width="40">
      </el-table-column>
      <el-table-column prop="itemId" header-align="center" align="center" label="">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.itemId" v-if="showInput == `itemId${scope.row.Id}`"
            @blur='blurInput(scope.row, "itemId", scope.row.itemId)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.itemId}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="orderId" header-align="center" align="center" label="订单id">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.orderId" v-if="showInput == `orderId${scope.row.Id}`"
            @blur='blurInput(scope.row, "orderId", scope.row.orderId)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.orderId}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="orderNo" header-align="center" align="center" label="订单编号">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.orderNo" v-if="showInput == `orderNo${scope.row.Id}`"
            @blur='blurInput(scope.row, "orderNo", scope.row.orderNo)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.orderNo}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="goodsId" header-align="center" align="center" label="药品Id">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.goodsId" v-if="showInput == `goodsId${scope.row.Id}`"
            @blur='blurInput(scope.row, "goodsId", scope.row.goodsId)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.goodsId}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="goodsCode" header-align="center" align="center" label="药品编号">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.goodsCode" v-if="showInput == `goodsCode${scope.row.Id}`"
            @blur='blurInput(scope.row, "goodsCode", scope.row.goodsCode)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.goodsCode}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="goodsName" header-align="center" align="center" label="药品名称">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.goodsName" v-if="showInput == `goodsName${scope.row.Id}`"
            @blur='blurInput(scope.row, "goodsName", scope.row.goodsName)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.goodsName}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="genericName" header-align="center" align="center" label="通用名">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.genericName" v-if="showInput == `genericName${scope.row.Id}`"
            @blur='blurInput(scope.row, "genericName", scope.row.genericName)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.genericName}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="standard" header-align="center" align="center" label="规格">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.standard" v-if="showInput == `standard${scope.row.Id}`"
            @blur='blurInput(scope.row, "standard", scope.row.standard)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.standard}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="unit" header-align="center" align="center" label="单位">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.unit" v-if="showInput == `unit${scope.row.Id}`"
            @blur='blurInput(scope.row, "unit", scope.row.unit)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.unit}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="producingArea" header-align="center" align="center" label="产地">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.producingArea" v-if="showInput == `producingArea${scope.row.Id}`"
            @blur='blurInput(scope.row, "producingArea", scope.row.producingArea)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.producingArea}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="produceFactory" header-align="center" align="center" label="生产单位">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.produceFactory" v-if="showInput == `produceFactory${scope.row.Id}`"
            @blur='blurInput(scope.row, "produceFactory", scope.row.produceFactory)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.produceFactory}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="approvalNo" header-align="center" align="center" label="批准文号">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.approvalNo" v-if="showInput == `approvalNo${scope.row.Id}`"
            @blur='blurInput(scope.row, "approvalNo", scope.row.approvalNo)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.approvalNo}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="dosageForm" header-align="center" align="center" label="剂型">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.dosageForm" v-if="showInput == `dosageForm${scope.row.Id}`"
            @blur='blurInput(scope.row, "dosageForm", scope.row.dosageForm)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.dosageForm}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="batchNo" header-align="center" align="center" label="批号">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.batchNo" v-if="showInput == `batchNo${scope.row.Id}`"
            @blur='blurInput(scope.row, "batchNo", scope.row.batchNo)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.batchNo}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="produceDate" header-align="center" align="center" label="生产日期">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.produceDate" v-if="showInput == `produceDate${scope.row.Id}`"
            @blur='blurInput(scope.row, "produceDate", scope.row.produceDate)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.produceDate}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="expiredDate" header-align="center" align="center" label="有效期">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.expiredDate" v-if="showInput == `expiredDate${scope.row.Id}`"
            @blur='blurInput(scope.row, "expiredDate", scope.row.expiredDate)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.expiredDate}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="purchaseNum" header-align="center" align="center" label="采购数量">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.purchaseNum" v-if="showInput == `purchaseNum${scope.row.Id}`"
            @blur='blurInput(scope.row, "purchaseNum", scope.row.purchaseNum)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.purchaseNum}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="purchasePrice" header-align="center" align="center" label="采购价">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.purchasePrice" v-if="showInput == `purchasePrice${scope.row.Id}`"
            @blur='blurInput(scope.row, "purchasePrice", scope.row.purchasePrice)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.purchasePrice}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="purchaseAmount" header-align="center" align="center" label="采购金额">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.purchaseAmount" v-if="showInput == `purchaseAmount${scope.row.Id}`"
            @blur='blurInput(scope.row, "purchaseAmount", scope.row.purchaseAmount)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.purchaseAmount}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="lastInPrice" header-align="center" align="center" label="上一次进价">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.lastInPrice" v-if="showInput == `lastInPrice${scope.row.Id}`"
            @blur='blurInput(scope.row, "lastInPrice", scope.row.lastInPrice)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.lastInPrice}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="oldRetailPrice" header-align="center" align="center" label="原零售价">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.oldRetailPrice" v-if="showInput == `oldRetailPrice${scope.row.Id}`"
            @blur='blurInput(scope.row, "oldRetailPrice", scope.row.oldRetailPrice)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.oldRetailPrice}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="retailPrice" header-align="center" align="center" label="零售价">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.retailPrice" v-if="showInput == `retailPrice${scope.row.Id}`"
            @blur='blurInput(scope.row, "retailPrice", scope.row.retailPrice)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.retailPrice}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="oldMemberPrice" header-align="center" align="center" label="原会员价">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.oldMemberPrice" v-if="showInput == `oldMemberPrice${scope.row.Id}`"
            @blur='blurInput(scope.row, "oldMemberPrice", scope.row.oldMemberPrice)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.oldMemberPrice}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="memberPrice" header-align="center" align="center" label="会员价">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.memberPrice" v-if="showInput == `memberPrice${scope.row.Id}`"
            @blur='blurInput(scope.row, "memberPrice", scope.row.memberPrice)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.memberPrice}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="wholeNum" header-align="center" align="center" label="整件数">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.wholeNum" v-if="showInput == `wholeNum${scope.row.Id}`"
            @blur='blurInput(scope.row, "wholeNum", scope.row.wholeNum)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.wholeNum}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="spreadNum" header-align="center" align="center" label="散装数">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.spreadNum" v-if="showInput == `spreadNum${scope.row.Id}`"
            @blur='blurInput(scope.row, "spreadNum", scope.row.spreadNum)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.spreadNum}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="storeId" header-align="center" align="center" label="门店id">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.storeId" v-if="showInput == `storeId${scope.row.Id}`"
            @blur='blurInput(scope.row, "storeId", scope.row.storeId)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.storeId}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="storeName" header-align="center" align="center" label="门店名称">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.storeName" v-if="showInput == `storeName${scope.row.Id}`"
            @blur='blurInput(scope.row, "storeName", scope.row.storeName)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.storeName}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="storeCode" header-align="center" align="center" label="门店编码">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.storeCode" v-if="showInput == `storeCode${scope.row.Id}`"
            @blur='blurInput(scope.row, "storeCode", scope.row.storeCode)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.storeCode}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.createTime" v-if="showInput == `createTime${scope.row.Id}`"
            @blur='blurInput(scope.row, "createTime", scope.row.createTime)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.createTime}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" header-align="center" align="center" label="修改时间">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.updateTime" v-if="showInput == `updateTime${scope.row.Id}`"
            @blur='blurInput(scope.row, "updateTime", scope.row.updateTime)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.updateTime}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createUsername" header-align="center" align="center" label="创建人">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.createUsername" v-if="showInput == `createUsername${scope.row.Id}`"
            @blur='blurInput(scope.row, "createUsername", scope.row.createUsername)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.createUsername}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="updateUsername" header-align="center" align="center" label="修改人">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.updateUsername" v-if="showInput == `updateUsername${scope.row.Id}`"
            @blur='blurInput(scope.row, "updateUsername", scope.row.updateUsername)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.updateUsername}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="是否可用 1:是">
        <template slot-scope="scope">
          <el-switch size="mini" v-model="scope.row.status" @change="saveHandle(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" label="备注">
        <template slot-scope="scope">
          <span v-html="scope.row.remark"></span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" v-if="isAuth('purchase:purchaseorderitem:save')"
            @click="saveHandle(scope.row)">保存</el-button>
          <el-button size="mini" type="text" v-if="isAuth('purchase:purchaseorderitem:update')"
            @click="addOrUpdateHandle(scope.row.itemId)">修改</el-button>
          <el-button size="mini" type="text" v-if="isAuth('purchase:purchaseorderitem:delete')"
            @click="deleteHandle(scope.row.itemId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './purchaseorderitem-add-or-update'
  import {
    downLoadFile
  } from '@/utils/myUtil.js'
  export default {
    data() {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        showInput: "",
        oldData: {},
        currentData: {},
      }
    },
    components: {
      AddOrUpdate
    },
    activated() {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      async getDataList() {
        this.dataListLoading = true
        const res = await this.$http({
          url: `/purchase/purchaseorderitem/list`,
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'dataForm': this.dataForm
          })
        })
        let data = res.data
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
          this.dataList.forEach(item => {
            item.status = item.status == 1 ? true : false
          })
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.itemId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const res = await this.$http({
            url: `/purchase/purchaseorderitem/delete`,
            method: 'post',
            data: this.$http.adornData(ids, false)
          })
          let data = res.data
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      resetForm() {
        this.$nextTick(() => {
          this.dataForm = this.$options.data().dataForm
          this.getDataList()
        })
      },
      async saveHandle(row) {
        row.status = row.status ? 1 : 0
        const res = await this.$http({
          url: `/purchase/purchaseorderitem/update`,
          method: 'post',
          data: this.$http.adornData(row)
        });
        let data = res.data
        if (data && data.code !== 0) {
          row.status = !row.status;
          return this.$message.error('修改失败!');
        }
        this.$message.success('更新成功！');
        this.getDataList();
      },
      // 当input失去光标后进行的操作
      async blurInput(row, name, value) {
        // 判断数据是否有所改变，如果数据有改变则调用修改接口
        if (this.oldData[name] != value) {
          row[name] = value
          if (value === true) {
            row[name] = 1
          } else if (value === false) {
            row[name] = 0
          }
        }
        this.showInput = ""
        this.currentData = row
      },
      tableDbEdit(row, column, event) {
        this.showInput = column.property + row.itemId;
        this.oldData[column.property] = row[column.property];
      },
      async exportData() {
        const res = await this.$http({
          url: `/purchase/purchaseorderitem/exportData`,
          method: 'get',
          responseType: 'blob',
          params: this.$http.adornParams()
        });
        downLoadFile(res)
      }
    },
    directives: {
      // 通过自定义指令实现的表单自动获得光标的操作
      focus: {
        inserted: function(el) {
          if (el.tagName.toLocaleLowerCase() == "input") {
            el.focus()
          } else {
            if (el.getElementsByTagName("input")) {
              el.getElementsByTagName("input")[0].focus()
            }
          }
          el.focus()
        }
      },
      focusTextarea: {
        inserted: function(el) {
          if (el.tagName.toLocaleLowerCase() == "textarea") {
            el.focus()
          } else {
            if (el.getElementsByTagName("textarea")) {
              el.getElementsByTagName("textarea")[0].focus()
            }
          }
          el.focus()
        }
      }
    },
  }
</script>
